
#include "mainwindow.h"
#include "qmediaplaylist.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->ButtonPause,&QPushButton::clicked,this,&MainWindow::SlotButtonStart);
    connect(ui->ButtonNextmusic,&QPushButton::clicked,this,&MainWindow::SlotButtonNext);
    connect(ui->ButtonLastmusic,&QPushButton::clicked,this,&MainWindow::SlotButtonPrevious);



    init();     //初始化相关函数
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::init()
{

    PlayerList = new QMediaPlaylist;    //实例化播放器和播放列表
    Player = new QMediaPlayer;


     fileList = getFileNames(this->MusicPath); //获取文件夹下所有音乐

    qDebug()<<"ALL Music File: \n"<<fileList;

    for(int i=0;i<fileList.size();i++)  //输出所有音频文件到界面
    {
        QString fileName=fileList.at(i);
        addItem(fileName);
        PlayerList->addMedia(QUrl::fromLocalFile(MusicPath+"\\"+fileName));
    }
    PlayerList->setCurrentIndex(0);

    Player->setPlaylist(PlayerList);    //将播放列表加载到播放器
    connect(Player,&QMediaPlayer::currentMediaChanged,this,&MainWindow::SlotShowCurrentMedia);

    connect(Player,SIGNAL(positionChanged(qint64)),this,SLOT(onPositionChanged(qint64)));
    connect(Player,SIGNAL(durationChanged(qint64)),this,SLOT(onDurationChanged(qint64)));

}
QStringList MainWindow::getFileNames(const QString &path)
{
    QDir dir(path);
    QStringList nameFilters;
    nameFilters << "*.mp3" ;
    QStringList files = dir.entryList(nameFilters, QDir::Files|QDir::Readable, QDir::Name);
    return files;
}
void MainWindow::SlotShowCurrentMedia()
{
    int index = PlayerList->currentIndex();
    QString songName = fileList.at(index);
    ui->label->setText(songName);
}
void MainWindow::SlotButtonStart()
{
    if(!isPlay)
    {
        isPlay = true;
        ui->ButtonPause->setStyleSheet("icon:url(:/stop.png);");
        Player->play();
    }
    else
    {
        isPlay = false;
        ui->ButtonPause->setStyleSheet("icon:url(:/start.png);");
        Player->pause();
        return;
    }
}
void MainWindow::SlotButtonPrevious()
{
    PlayerList->previous();
    PlayerList->previousIndex();
}
void MainWindow::SlotButtonNext()
{
    PlayerList->next();
    PlayerList->nextIndex();
}
void MainWindow::addItem(QString name)
{
    int count = ui->tableWidget->rowCount();
    ui->tableWidget->setRowCount(count+1);
    QTableWidgetItem *itemName = new QTableWidgetItem(name);
    ui->tableWidget->setItem(count,0,itemName);
}
void MainWindow::onDurationChanged(qint64 duration)
{
    ui->horizontalSlider_2->setMaximum(duration); //设置进度条最大值 也就是歌曲时长 ms
    int secs = duration/1000; //全部秒数
    int mins = secs/60;//分
    secs = secs % 60;//秒
    durationTime = QString::asprintf("%d:%d",mins,secs);
    ui->label_2->setText(positionTime+"/"+durationTime);
}
void MainWindow::onPositionChanged(qint64 position)
{
    if(ui->horizontalSlider_2->isSliderDown())
        return;//如果手动调整进度条，则不处理
    ui->horizontalSlider_2->setSliderPosition(position);
    int secs = position/1000;
    int mins = secs/60;
    secs = secs % 60;
    positionTime = QString::asprintf("%d:%d",mins,secs);
    ui->label_2->setText(positionTime+"/"+durationTime);
}
void MainWindow::on_horizontalSlider_2_valueChanged(int value)
{
    Player->setPosition(ui->horizontalSlider_2->value());//文件播放位置 根据进度条传入的参数
}





void MainWindow::on_horizontalSlider_valueChanged(int value)//音量
{
    Player->setVolume(value);
}

//暂停图表更换的尝试
//void MainWindow::on_ButtonPause_clicked()
//{
//if(ui->ButtonPause->styleSheet()=="icon:url(:/stop.png);")
//    //{
//        ui->ButtonPause->styleSheet()="icon:url(:/start.png);";
//    }
//    else
//    {
//        ui->ButtonPause->styleSheet()="icon:url(:/stop.png);";
//    }
//}




void MainWindow::on_pushButton_2_clicked()
{

}

